<template>
  <div class="editForm-wrapper">
    <el-dialog :title="title" :close-on-click-modal="false" v-dialogDrag :visible.sync="visible">
      <el-form :model="inputForm" ref="inputForm" v-loading="loading" :class="method==='view'?'readonly':''" :disabled="method==='view'"
        label-width="96px">
        <el-row :gutter="15">
          <el-col :span="12">
            <el-form-item label="类型:" prop="types" :rules="[
                  {required: true, message:'类型不能为空', trigger:'blur'}
                 ]">
              <el-radio-group v-model="types">
                <el-radio v-for="item in $dictUtils.getDictList('companyType')" v-if="item.value==='2'" che :key="item.value" :label="Number(item.value)">{{ item.label }}</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="15">
          <el-col :span="12">
            <el-form-item label="姓名:" prop="names" :rules="[
                  {required: true, message:'姓名不能为空', trigger:'blur'},
                  {validator: validator.isName, trigger:'blur'},
                  {validator: getCompanyClientByName, trigger:'blur'}
                 ]">
              <el-input v-model="inputForm.names" placeholder="请填写姓名"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="15">
          <el-col :span="50">
            <el-form-item label="手机号:" prop="contactPhone" :rules="[
                {required: true, message:'手机号不能为空', trigger:'blur'},
                {validator: validator.isMobile, trigger:'blur'}
               ]">
              <el-input v-model="inputForm.contactPhone" placeholder="请填写手机号"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="15">
          <el-col :span="50">
            <el-form-item label="身份证号:" prop="idCard" :rules="[
                {required: true, message:'身份证号不能为空', trigger:'blur'},
                {validator: validator.isCardId, trigger:'blur'},
                  {validator: getCompanyClientByidCard, trigger:'blur'}
               ]">
              <el-input v-model="inputForm.idCard" placeholder="请填写身份证号"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <div class="footer-input">
          <el-button type="primary" v-if="method != 'view'" @click="doSubmit()" v-noMoreClick>确定</el-button>
          <el-button @click="visible = false">取消</el-button>
        </div>
      </span>
    </el-dialog>
    <CompanyDialog ref="CompanyDialog" @getCompanyData="getCompanyData"></CompanyDialog>
  </div>
</template>

<script>
  import CompanyDialog from '@/components/dialog/queryAllCompanyDialog.vue'
  export default {
    data() {
      return {
        title: '',
        method: '',
        visible: false,
        loading: false,
        inputForm: {
          id: '',
          names: '',
          contactPhone: '',
          idCard: '',
          addressId: ''
        },
         types:'2'
      }
    },
    components: {
      CompanyDialog
    },
    methods: {
    	handleClose(done) {
			this.$Msg.confirm({
				title:'您确定要关闭吗？',
				content:this.GLOBAL.FORM_CLOSE_TIPS,
				confirmCall:()=>{
					done();
				}
			})
		},
      init(method, obj) {
        this.method = method
        this.types = 2
        if (method === 'add') {
          this.title = `新增车主`
        } else if (method === 'edit') {
          this.title = '修改车主'
          this.inputForm.id = obj.id
        } else if (method === 'view') {
          this.title = '查看车主'
          this.inputForm.id = obj.id
        }
        this.visible = true
        this.loading = false
        this.$nextTick(() => {
          this.$refs.inputForm.resetFields()
          if (method === 'edit') { // 修改或者查看
            this.loading = true
            this.$http({
              url: `/truck/ower/queryById?id=${this.inputForm.id}`,
              method: 'get'
            }).then(({
              data
            }) => {
              if (data.success) {
                this.inputForm = this.recover(this.inputForm, data.result)
                this.loading = false
              } else {
                this.loading = false
                this.visible = false
              }
            })
          }
        })
      },
      // 往来选择弹窗
      getCompanyClientByName () {
        if (this.inputForm.names){
          this.$nextTick(()=>{
            this.$refs['CompanyDialog'].getCompanyList(this.$refs['CompanyDialog'].page,this.inputForm.names)
            this.$refs['CompanyDialog'].dialogVisible = true
          })

        }
      },
      // 往来选择弹窗
      getCompanyClientByidCard () {
        if (this.inputForm.idCard){
          this.$nextTick(()=>{
            this.$refs['CompanyDialog'].getCompanyList(this.$refs['CompanyDialog'].page, '', this.inputForm.idCard)
            this.$refs['CompanyDialog'].dialogVisible = true
          })
        }
      },
      // 客户选择数据回显
      getCompanyData (row) {
        this.inputForm.names = row.names
        this.inputForm.contactPhone = row.contactPhone
        this.inputForm.idCard = row.idCard
        this.dialogVisible = false
      },
      // 表单提交
      doSubmit() {
        this.$refs['inputForm'].validate((valid) => {
          if (valid) {
            this.loading = true
            this.$http({
              url: `/truck/ower/save`,
              method: 'post',
              data: this.inputForm,
              headers: {
                'Content-Type': 'application/json; charset=utf-8'
              }
            }).then(({
              data
            }) => {
              if (data && data.code === '200') {
                this.visible = false
                this.$message.success(data.message)
                this.$emit('refreshDataList')
              }
            })
          }
        })
      }
    }
  }
</script>
<style lang="scss" scoped>
  .editForm-wrapper {
    ::v-deep.el-dialog {
      .el-input__inner {
        width: 320px !important;
      }

      .el-dialog__body .el-form .el-form-item {
        margin-bottom: 20px !important;
        margin-right: 0;
      }
    }
  }
</style>
